<?php

/**
 * 阿里云 生活物联网接口测试
 */

namespace app\shell;

use app\service\AliyunCloudStorageService;
use app\service\AliyunService;
use app\service\OpenAppOrderService;
use app\service\OssOrderService;
use share\console\CConsole;
use share\console\CLIRunner;

class CAliyunOss extends CLIRunner{

    /**
     * 同步套餐信息
     */
    public function async_product_action(){
        $sites = array_keys(AliyunService::allHost());
        $projectId = AliyunService::getDefaultProjectId();
        $res  = [];
        foreach($sites as $site){
            $handle = AliyunService::instance($projectId,$site);
            $res[$site] = $handle->getCloudStorage();
        }
        foreach($res as $site => $items){
            foreach($items as $item){
                // 门铃设备相关的云存  不需要
                if(strpos($item['specification'],'doorbell') !== false){
                    continue;
                }
                $formatItem = self::formatCloudStorageProductInfo($item);
                AliyunCloudStorageService::updateByRemote($site,$formatItem);
            }
            
        }
        return $res;
    }

    public static function formatCloudStorageProductInfo($item){
        $res = [
            "lifecycle" => $item['lifecycle'],
            "months" => $item['months'],
            "record_type" => $item['recordType'],
            "price" => round(floatval($item['price']) * 100),
            "commodity_code" => $item['commodityCode'],
            "specification" => $item['specification'],
            "commodity_name" => $item['commodityName'],
        ];
        return $res;
    }


    public static function create_order_action(){
        $orderInfo = [
            'uid' => 26928,
            'iotId' => 'WpC4u9buiw6rs57BCK0j000000',
            // 23092211580589801004 30天循环事件云存储月套餐
            // 23122211243937901008 30天循环连续云存储月套餐
            'sku' => '23092211580589801004', 
            'outTradeNo'=> date("YmdHis").mt_rand(1000,9999),
            'num' => 1,
        ];
        return OpenAppOrderService::create(1,$orderInfo);
    }

    public static function buy_order_action($orderNo = '',$openAppId = 2){
        if(empty($orderNo)){
            CConsole::print_line("need a order_no least");
            return;
        }
        return OpenAppOrderService::buy($openAppId ,$orderNo);        
        // OssOrderService::update(10001,[
        //     'state' => OssOrderService::STATE_OF_PAYED,
        //     'pay_order_no' => '202309191447277240001',
        //     'pay_time' => time(),
        // ]);
    }

    public static function async_payed_device_info_action($iotId = 'WpC4u9buiw6rs57BCK0j000000'){
        return OpenAppOrderService::orderAsync($iotId);
    }

    public static function get_payed_info_by_sn_action($sn = '038900020048314f77bf'){
        return OpenAppOrderService::payedInfo(1,$sn);
    }

    public static function get_free_product_action($iotId = "WpC4u9buiw6rs57BCK0j000000") {
        $projectId = AliyunService::getDefaultProjectId();
        $site = 'CN';
        $handle = AliyunService::instance($projectId,$site);
        return $handle->getFreeCloudStorage($iotId);
    }

    public static function buy_free_product_action($iotId = "WpC4u9buiw6rs57BCK0j000000") {
        $projectId = AliyunService::getDefaultProjectId();
        $site = 'CN';
        $handle = AliyunService::instance($projectId,$site);
        return $handle->consumeFreeCloudStorage($iotId);
    }

    public static function query_buyed_action($iotId = 'WpC4u9buiw6rs57BCK0j000000') {
        $projectId = AliyunService::getDefaultProjectId();
        $site = 'CN';
        $handle = AliyunService::instance($projectId,$site);
        return $handle->cloudStorageOrderQuery($iotId,0,50);
    }
}